added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CSWebBrowserSuppressError / WebBrowserEx.cs
blobac1e6abe9915434d77996f9021654eb6926b82ce
1 /****************************** Module Header ******************************\
2 * Module Name: WebBrowserEx.cs
3 * Project: CSWebBrowserSuppressError
4 * Copyright (c) Microsoft Corporation.
5 *
6 * This WebBrowserEx class inherits WebBrowser class and supplies following
7 * features.
8 * 1. Disable JIT Debugger.
9 * 2. Suppress html element errors of document loaded in this browser.
10 * 3. Handle navigation error.
12 * The class WebBrowser itself also has a Property ScriptErrorsSuppressed to hides
13 * all its dialog boxes that originate from the underlying ActiveX control, not
14 * just script errors.
16 * This source is subject to the Microsoft Public License.
17 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
18 * All other rights reserved.
20 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
21 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
23 \***************************************************************************/
25 using System;
26 using System.Security.Permissions;
27 using System.Windows.Forms;
28 using Microsoft.Win32;
31 namespace CSWebBrowserSuppressError
33 public partial class WebBrowserEx : WebBrowser
35 /// <summary>
36 /// Get or Set whether JIT debugger needs to be disabled. You have to restart the
37 /// browser to take effect.
38 /// </summary>
39 public static bool JITDebuggerDisabled
41 get
43 using (RegistryKey ieMainKey = Registry.CurrentUser.OpenSubKey(
44 @"Software\Microsoft\Internet Explorer\Main"))
46 string keyvalue = ieMainKey.GetValue("Disable Script Debugger") as string;
47 return string.Equals(keyvalue, "yes", StringComparison.OrdinalIgnoreCase);
50 set
52 var newValue = value ? "yes" : "no";
54 using (RegistryKey ieMainKey = Registry.CurrentUser.OpenSubKey(
55 @"Software\Microsoft\Internet Explorer\Main", true))
57 string keyvalue = ieMainKey.GetValue("Disable Script Debugger") as string;
58 if (!keyvalue.Equals(newValue, StringComparison.OrdinalIgnoreCase))
60 ieMainKey.SetValue("Disable Script Debugger", newValue);
66 // Suppress html element errors.
67 public bool HtmlElementErrorsSuppressed { get; set; }
69 AxHost.ConnectionPointCookie cookie;
71 WebBrowser2EventHelper helper;
73 public event EventHandler<WebBrowserNavigateErrorEventArgs> NavigateError;
75 [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
76 public WebBrowserEx()
80 /// <summary>
81 /// Register the Document.Window.Error event.
82 /// </summary>
83 [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
84 protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
86 base.OnDocumentCompleted(e);
88 // Occurs when script running inside of the window encounters a run-time error.
89 this.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
93 /// <summary>
94 /// Handle html element errors of document loaded in this browser.
95 /// If HtmlElementErrorsSuppressed is set to true, then set the
96 /// Handled flag to true sothat browser will not display this error.
97 /// </summary>
98 protected void Window_Error(object sender, HtmlElementErrorEventArgs e)
100 if (HtmlElementErrorsSuppressed)
102 e.Handled = true;
107 /// <summary>
108 /// Associates the underlying ActiveX control with a client that can
109 /// handle control events including NavigateError event.
110 /// </summary>
111 [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
112 protected override void CreateSink()
114 base.CreateSink();
116 helper = new WebBrowser2EventHelper(this);
117 cookie = new AxHost.ConnectionPointCookie(
118 this.ActiveXInstance, helper, typeof(DWebBrowserEvents2));
122 /// <summary>
123 /// Releases the event-handling client attached in the CreateSink method
124 /// from the underlying ActiveX control
125 /// </summary>
126 [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
127 protected override void DetachSink()
129 if (cookie != null)
131 cookie.Disconnect();
132 cookie = null;
134 base.DetachSink();
138 /// <summary>
139 /// Raises the NavigateError event.
140 /// </summary>
141 protected virtual void OnNavigateError(WebBrowserNavigateErrorEventArgs e)
143 if (this.NavigateError != null)
145 this.NavigateError(this, e);